//
//  HeroCell.m
//  CandyCrushClone
//
//  Created by Petar on 5/8/15.
//  Copyright (c) 2015 Nestorov Engineering. All rights reserved.
//

#import "HeroInfoCell.h"

@interface HeroInfoCell ()

@property (weak, nonatomic) IBOutlet UIButton *heroButton;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *producesLabel;
@property (weak, nonatomic) IBOutlet UILabel *attackLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UIImageView *nameImageView;
@property (weak, nonatomic) IBOutlet UIImageView *priceImageView;
@property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView;
@property (weak, nonatomic) IBOutlet UIImageView *heroFrameImageView;
@property (weak, nonatomic) IBOutlet UIImageView *separatorImageView;
@property (weak, nonatomic) IBOutlet UIImageView *producesCookiesImageView;
@property (weak, nonatomic) IBOutlet UIImageView *attackCookiesImageView;
@property (weak, nonatomic) IBOutlet UIButton *upgradeAttackButton;
@property (weak, nonatomic) IBOutlet UIButton *upgradeProducesButton;
@property (weak, nonatomic) IBOutlet UIButton *padlockBuyButton;
@property (weak, nonatomic) IBOutlet UIButton *infoButton;

@end

@implementation HeroInfoCell
{
    NSIndexPath *_indexPath;
    BOOL _isAlreadyBought;
    HeroInfo *_heroInfo;
    NSString *_colorPrefix;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
    
}


- (void)configureWithHeroInfo:(HeroInfo *)heroInfo forIndexPath:(NSIndexPath *)indexPath
{
    
    _indexPath = indexPath;
    _heroInfo = heroInfo;
    
    if ( heroInfo.isOwned == NO ) {
        
        _colorPrefix = @"red";
        [self configureViewsForLockedHero];
        
    } else {
        
        [self configureViewsForUnlockedHero];
        
        if ( heroInfo.isChosen == YES ) {
            
            _colorPrefix = @"green";
            [self showButton:_infoButton];
            
        } else {
            
            _colorPrefix = @"blue";
            [self hideButton:_infoButton];
            
        }
        
    }
    
    [self setImages];
    
    NSString *heroImageName = [NSString stringWithFormat:@"animal_%zd", heroInfo.type];
    UIImage *heroImage = [UIImage imageNamed:heroImageName];
    [_heroButton setBackgroundImage:heroImage forState:UIControlStateNormal];
    _nameLabel.text = heroInfo.name;
    _priceLabel.text = [NSString stringWithFormat:@"%zd", heroInfo.price];
    _producesLabel.text = [NSString stringWithFormat:@"%zd x", heroInfo.produces];
    _attackLabel.text = [NSString stringWithFormat:@"%zd x", ABS(heroInfo.attack)];
    
}


- (void)configureViewsForLockedHero
{
    
    [self hideButton:_infoButton];
    [self showButton:_padlockBuyButton];
    
    [self disableButton:_upgradeProducesButton];
    [self disableButton:_upgradeAttackButton];
    
    [_priceImageView setHidden:NO];
    [_priceLabel setHidden:NO];
    
}


- (void)configureViewsForUnlockedHero
{
    
    [_priceImageView setHidden:YES];
    [_priceLabel setHidden:YES];
    
    [self hideButton:_padlockBuyButton];
    
    NSInteger maxLevel = 7;
    
    // Enable Upgrade button if produces level is lower than maxLevel
    if ( _heroInfo.produces < maxLevel ) {
        
        [self enableButton:_upgradeProducesButton];
        
    } else {
        
        [self disableButton:_upgradeProducesButton];
        
    }
    
    // Enable Upgrade button if attack level is lower than maxLevel
    if ( _heroInfo.attack > -maxLevel ) {
        
        [self enableButton:_upgradeAttackButton];
        
    } else {
        
        [self disableButton:_upgradeAttackButton];
        
    }
    
}


- (void)setImages
{
    
    NSString *nameImageName = [NSString stringWithFormat:@"%@NamePad",_colorPrefix];
    UIImage *nameImage = [UIImage imageNamed:nameImageName];
    _nameImageView.image = nameImage;
    
    NSString *priceImageName = [NSString stringWithFormat:@"%@MoneyPad",_colorPrefix];
    UIImage *priceImage = [UIImage imageNamed:priceImageName];
    _priceImageView.image = priceImage;
    
    NSString *backgroundImageName = [NSString stringWithFormat:@"%@CellBackground",_colorPrefix];
    UIImage *backgroundImage = [UIImage imageNamed:backgroundImageName];
    _backgroundImageView.image = backgroundImage;
    
    NSString *heroFrameImageName = [NSString stringWithFormat:@"%@CellHeroFrame",_colorPrefix];
    UIImage *heroFrameImage = [UIImage imageNamed:heroFrameImageName];
    _heroFrameImageView.image = heroFrameImage;
    
    NSString *separatorImageName = [NSString stringWithFormat:@"%@Separator",_colorPrefix];
    UIImage *separatorImage = [UIImage imageNamed:separatorImageName];
    _separatorImageView.image = separatorImage;
    
    NSString *cookiesImageName = [NSString stringWithFormat:@"%@StoreCookies",_colorPrefix];
    UIImage *cookiesImage = [UIImage imageNamed:cookiesImageName];
    _producesCookiesImageView.image = cookiesImage;
    _attackCookiesImageView.image = cookiesImage;
    
    NSString *upgradeImageName = [NSString stringWithFormat:@"%@UpgradeButtonPad",_colorPrefix];
    UIImage *upgradeImage = [UIImage imageNamed:upgradeImageName];
    [_upgradeAttackButton setBackgroundImage:upgradeImage forState:UIControlStateNormal];
    [_upgradeProducesButton setBackgroundImage:upgradeImage forState:UIControlStateNormal];
    
}


- (IBAction)didPressInfoButton:(id)sender
{
    
    [_delegate didPressInfoButton];
    
}


- (IBAction)didPressBuyHeroButton:(id)sender
{
    
    [_delegate didPressBuyHeroButtonAtIndexPath:_indexPath];
    
}


- (IBAction)didPressUpgradeAttackButton:(id)sender
{
    
    [_delegate didPressUpgradeAttackButtonAtIndexPath:_indexPath];
    
}


- (IBAction)didPressUpgradeProduceButton:(id)sender
{
    
    [_delegate didPressUpgradeProduceButtonAtIndexPath:_indexPath];
    
}


- (IBAction)didPressHeroButton:(id)sender
{
    
    if ( _heroInfo.isOwned == NO || _heroInfo.isChosen == YES ) {
        return;
    }
    
    [_delegate didPressHeroButtonAtIndexPath:_indexPath];
    
}

#pragma mark - Helper methods
- (void)showButton:(UIButton *)button
{
    
    [button setHidden:NO];
    [button setEnabled:YES];
    
}


- (void)hideButton:(UIButton *)button
{
    
    [button setHidden:YES];
    [button setEnabled:NO];
    
}


- (void)enableButton:(UIButton *)button
{
    
    [button setHighlighted:NO];
    [button setEnabled:YES];
    
}


- (void)disableButton:(UIButton *)button
{
    
    [button setHighlighted:YES];
    [button setEnabled:NO];
    
}

@end
